package com.cs4261.smartalarm.frontend;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.cs4261.smartalarm.R;
import com.cs4261.smartalarm.backend.Alarm;
import com.cs4261.smartalarm.backend.AlarmReceiver;
import com.cs4261.smartalarm.backend.AlarmService;
import com.cs4261.smartalarm.backend.AlarmsDatabaseHelper;
import com.cs4261.smartalarm.component.AlarmAdapter;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

/**
 * @author Joshua
 *
 */
public class ManageAlarmsActivity extends Activity {
	Intent alarmService;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_manage_alarms);
		
		buildAlarmList();
		
		if(alarmService == null) alarmService = new Intent(getApplicationContext(), AlarmService.class);
		getApplicationContext().startService(alarmService);
	}

	/**
	 * 
	 */
	private void buildAlarmList() {
		AlarmsDatabaseHelper alarmDBHelper = new AlarmsDatabaseHelper(getApplicationContext());
		alarmDBHelper.open();
		List<Alarm>  alarmList = alarmDBHelper.getAllAlarms();
		alarmDBHelper.close();
				
		ListView myListView = (ListView) findViewById(R.id.listView1);
		AlarmAdapter customAdapter;
		
		if(alarmList.size() > 0) {
			customAdapter = new AlarmAdapter(this, R.layout.alarm_entity, alarmList);
		}
		else {
			customAdapter = new AlarmAdapter(this, R.layout.alarm_entity);
		}
		
		myListView.setAdapter(customAdapter);
	}
	
	/**
	 * @param view
	 */
	public void onNewAlarm(View view) {
		//Start a new Intent
		Intent intent = new Intent(ManageAlarmsActivity.this, CreateAlarmActivity.class);
	    startActivity(intent);
	}
	
	/**
	 * @param view
	 */
	public void onToggleAlarm(View view) {
		AlarmsDatabaseHelper alDBHelper = new AlarmsDatabaseHelper(getApplicationContext());
		alDBHelper.open();
		Alarm alarm = alDBHelper.getAlarmById(Long.parseLong(((TextView)((View) view.getParent().getParent().getParent()).findViewById(R.id.alarm_id)).getText().toString()));
		Intent intent = new Intent(this, AlarmReceiver.class);
		PendingIntent sender = PendingIntent.getBroadcast(this, (int) alarm.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		
		if(alarm.getActive() == 0) {
			alarm.setActive(1);
			Date dat = new Date();//initializes to now
		    Calendar cal_alarm = Calendar.getInstance();
		    Calendar cal_now = Calendar.getInstance();
		    cal_now.setTime(dat);
		    cal_alarm.setTime(dat);
		    cal_alarm.set(Calendar.HOUR_OF_DAY, alarm.getHour());//set the alarm time
		    cal_alarm.set(Calendar.MINUTE, alarm.getMinute());
		    cal_alarm.set(Calendar.SECOND, 0);
		    if(cal_alarm.before(cal_now)){//if its in the past increment
		        cal_alarm.add(Calendar.DATE,1);
		    }
		    alarmManager.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), sender);
		}
		else {
			alarm.setActive(0);
			alarmManager.cancel(sender);
		}
		alDBHelper.updateAlarm(alarm);		
		alDBHelper.close();
		



		
		
	}
	
	/**
	 * @param view
	 */
	public void onToggleDetails(View view) {
		 View detail_layout = ((View) view.getParent()).findViewById(R.id.detail_layout);
		 
		 if(detail_layout.getVisibility() == View.GONE) {
			 detail_layout.setVisibility(View.VISIBLE);
		 }
		 else {
			 detail_layout.setVisibility(View.GONE);
		 }
	}
	
	/**
	 * @param view
	 */
	public void onEditAlarm(View view) {
		Intent intent = new Intent(ManageAlarmsActivity.this, EditAlarmActivity.class);
		intent.putExtra("alarm_id", ((TextView) ((View) view.getParent().getParent().getParent().getParent()).findViewById(R.id.alarm_id)).getText().toString());
	    startActivity(intent);
	}
	
	/**
	 * @param view
	 */
	public void onDeleteAlarm(View view) {
		TextView alarm_id = (TextView) ((View) view.getParent().getParent().getParent().getParent()).findViewById(R.id.alarm_id);
		String id = alarm_id.getText().toString();
		
		AlarmsDatabaseHelper alarmDBHelper = new AlarmsDatabaseHelper(getApplicationContext());
		alarmDBHelper.open();
		alarmDBHelper.deleteAlarm(Long.parseLong(id));
		alarmDBHelper.close();
		
		Intent intent = new Intent(this, AlarmReceiver.class);
		PendingIntent sender = PendingIntent.getBroadcast(this, (int) Long.parseLong(id), intent, PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		alarmManager.cancel(sender);		
		
		buildAlarmList();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_manage_alarms, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		
		case R.id.menu_manage_locations:
			Intent newIntent = new Intent(this, ManageLocations.class);
			startActivity(newIntent);
		}	
		return super.onOptionsItemSelected(item);
	}
}
